package com.net.xpay.admin.controller.admin;

import com.google.common.base.Preconditions;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultPagedList;
import com.net.xpay.common.domain.UserShard;
import com.net.xpay.common.manager.UserShardManager;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.safePay.BookAccountChargeOrder;
import com.net.xpay.core.manager.safePay.BookAccountChargeOrderManager;
import com.net.xpay.core.model.BookAccountChargeOrderSearchModel;
import com.net.shard.ShardUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**

 */
@RestController
@Slf4j
@RequestMapping("/admin/safe-pay-book-account-order")
@Api(tags = "后台/安全付备付金充值记录", description = "后台 安全付备付金充值记录 net")
public class AdminBookAccountChargeOrderController {

    @Autowired
    private BookAccountChargeOrderManager bookAccountChargeOrderManager;

    @Autowired
    private UserShardManager userShardManager;

    /**
     * 分页
     */
    @ApiOperation("分页")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'safe-pay-book-account-order::search')")
    public ResultPagedList<BookAccountChargeOrder> search(@RequestBody BookAccountChargeOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        if (searchModel.getUserId() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "商户编号必传");
        }

        UserShard userShard = userShardManager.getById(searchModel.getUserId());
        if (userShard == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "商户不存在");
        }



        try {
            ShardUtil.setShardContext(userShard.getShardId());
            List<BookAccountChargeOrder> records = bookAccountChargeOrderManager.search(searchModel);

            long count = bookAccountChargeOrderManager.searchCount(searchModel);

            return new ResultPagedList<>(records, count, searchModel);
        } finally {
            ShardUtil.cleanShardContext();
        }
    }
}
